package com.example.springboot2022nocv.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springboot2022nocv.entity.ChinaTotal;
import com.example.springboot2022nocv.entity.LineTrend;
import com.example.springboot2022nocv.entity.NocvData;
import com.example.springboot2022nocv.entity.NocvNews;
import com.example.springboot2022nocv.service.ChinaTotalService;
import com.example.springboot2022nocv.service.IndexService;
import com.example.springboot2022nocv.service.NocvNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import java.text.ParseException;
import java.util.*;

/**
 * @author 程序员小洋
 * @version 2022.3
 * @date 2022/12/22 17:14
 * @title 首页的控制层
 */
@SuppressWarnings("all")
@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;

    @Autowired
    private ChinaTotalService chinaTotalService;

    @Autowired
    private NocvNewsService nocvNewsService;

    /**
     * 主控页面
     * @param model
     * @return
     * @throws ParseException
     */
    @RequestMapping("/")
    public String index(Model model) throws ParseException {

        // 1.找到ID最大的那一条数据
        Integer id = chinaTotalService.maxID();
        // 2.根据ID进行查找数据
        ChinaTotal chinaTotal = chinaTotalService.getById(id);
        model.addAttribute("chinaTotal",chinaTotal);
        return "index";
    }

    @RequestMapping("/toChina")
    public String toChina(Model model) throws ParseException {

        // 1.找到ID最大的那一条数据
        Integer id = chinaTotalService.maxID();

        // 2.根据ID进行查找数据

        // redis查询数据库的逻辑
        // 1.查询redis缓存[有数据,直接返回] [没有数据,查询mysql数据库,更新缓存,返回客户端]
        Jedis jedis = new Jedis("127.0.0.1");

        // 2.1拿到客户端连接
        if (jedis!=null) {
            String confirm = jedis.get("confirm");
            String input = jedis.get("input");
            String heal = jedis.get("heal");
            String dead = jedis.get("dead");
            String updateTime = jedis.get("updateTime");
            // 2.2缓存里面有数据
            if (StringUtils.isNotBlank(confirm)
                    && StringUtils.isNotBlank(input)
                    && StringUtils.isNotBlank(heal)
                    && StringUtils.isNotBlank(dead)
                    && StringUtils.isNotBlank(updateTime)){
                ChinaTotal chinaTotalRedis = new ChinaTotal();
                chinaTotalRedis.setConfirm(Integer.parseInt(confirm));
                chinaTotalRedis.setInput(Integer.parseInt(input));
                chinaTotalRedis.setHeal(Integer.parseInt(heal));
                chinaTotalRedis.setDead(Integer.parseInt(dead));
                // 格式调整 String ----> Date
                chinaTotalRedis.setUpdateTime(new Date());
                System.out.println("redis中的数据：" + chinaTotalRedis);
                // 扔回前台
                model.addAttribute("chinaTotal",chinaTotalRedis);
                // 3.疫情播报新闻
                List<NocvNews> newsList = nocvNewsService.listNewsLimit5();
                model.addAttribute("newsList",newsList);
                return "china";
            }else{
                // 2.3缓存里面没有数据 查询数据
                ChinaTotal chinaTotal = chinaTotalService.getById(id);
                model.addAttribute("chinaTotal",chinaTotal);
                // 3.疫情播报新闻
                List<NocvNews> newsList =nocvNewsService.listNewsLimit5();
                model.addAttribute("newsList",newsList);
                // 2.4 更新缓存
                jedis.set("confirm",String.valueOf(chinaTotal.getConfirm()));
                jedis.set("input",String.valueOf(chinaTotal.getInput()));
                jedis.set("heal",String.valueOf(chinaTotal.getHeal()));
                jedis.set("dead",String.valueOf(chinaTotal.getDead()));
                jedis.set("updateTime",String.valueOf(chinaTotal.getUpdateTime()));
                return "china";
            }
        }
        ChinaTotal chinaTotal = chinaTotalService.getById(id);
        model.addAttribute("chinaTotal",chinaTotal);
        // 3.疫情播报新闻
        List<NocvNews> newsList =nocvNewsService.listNewsLimit5();
        model.addAttribute("newsList",newsList);
        return "china";
    }

    @RequestMapping("/query")
    @ResponseBody
    public List<NocvData> queryData() throws ParseException {

        /**
         * 每天更新一次的数据使用场景
         */
        // QueryWrapper<NocvData> queryWrapper = new QueryWrapper<>();
        // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // String format1 = format.format(new Date());
        // queryWrapper.ge("update_time",format.parse(format1));
        // List<NocvData> list = indexService.list();

        // 1.先查redis缓存 有数据返回即可
        Jedis jedis = new Jedis("127.0.0.1");
        if (jedis != null) {
            // 1.1 有缓存数据 返回数据即可
            List<String> listRedis = jedis.lrange("nocvdata", 0, 33);
            List<NocvData> dataList = new ArrayList<>();
            if (listRedis.size() > 0) {
                for (int i = 0; i < listRedis.size(); i++) {
                    System.out.println("列表项为: " + listRedis.get(i));
                    String s = listRedis.get(i);
                    JSONObject jsonObject = JSONObject.parseObject(s);
                    Object name = jsonObject.get("name");
                    Object value = jsonObject.get("value");
                    NocvData nocvData = new NocvData();
                    nocvData.setName(String.valueOf(name));
                    nocvData.setValue(Integer.parseInt(value.toString()));
                    dataList.add(nocvData);
                }
                // 查询redis缓存数据库 返回的数据
                return dataList;
            }
        } else {
                // 1.2 redis没有数据 查Mysql数据库,更新缓存
                List<NocvData> list = indexService.listOrderByIdLimit34();
                for (NocvData nocvData : list) {
                    jedis.lpush("nocvdata", JSONObject.toJSONString(nocvData));
                }
                // 返回的数据中的数据
                return list;
            }
            // 默认没有连接redis的返回数据库【兼容有没有安装redis】
            List<NocvData> list = indexService.listOrderByIdLimit34();
            return list;
        }

    // 跳转饼状图页面
    @RequestMapping("/toPie")
    public String toPie(){
        return "pie";
    }

    /**
     * 分组聚合
     * select count(*) from goods group by type
     */
    @RequestMapping("/queryPie")
    @ResponseBody
    public List<NocvData> queryPieData(){
        List<NocvData> list = indexService.listOrderByIdLimit34();
        return list;
    }

    /**
     * 跳转柱状图页面
     */
    @RequestMapping("/toBar")
    public String toBar(){
        return "bar";
    }

    @RequestMapping("/queryBar")
    @ResponseBody
    public Map<String,List<Object>> queryBarData(){

        // 1.所有城市数据: 数值
        List<NocvData> list = indexService.listOrderByIdLimit34();

        // 2.所有城市数据
        List<String> cityList = new ArrayList<>();
        for (NocvData data : list) {
            cityList.add(data.getName());
        }

        // 3.所有疫情数值数据
        List<Integer> dataList = new ArrayList<>();
        for (NocvData data : list) {
            dataList.add(data.getValue());
        }

        // 4.创建Map
        Map map =new HashMap();
        map.put("cityList",cityList);
        map.put("dataList",dataList);
        return map;
    }

    /**
     * 跳转到折线图页面
     */
    @RequestMapping("/toLine")
    public String toLine(){
        return "line";
    }

    /**
     * 折线图数据接口
     */
    @RequestMapping("/queryLine")
    @ResponseBody
    public Map<String,List<Object>> queryLineData(){

        // 1.近七天查询所有的数据
        List<LineTrend> list7Day = indexService.findSevenData();

        // 2.封装所有的确诊人数
        List<Integer> confirmList = new ArrayList<>();

        // 3.封装所有隔离的人数
        List<Integer> isolationList = new ArrayList<>();

        // 4.封装所有的治愈人数
        List<Integer> cureList = new ArrayList<>();

        // 5.封装所有的死亡人数
        List<Integer> deadList = new ArrayList<>();

        // 6.封装所有的疑似人数
        List<Integer> similarList = new ArrayList<>();

        for (LineTrend data : list7Day) {
            confirmList.add(data.getConfirm());
            isolationList.add(data.getIsolation());
            cureList.add(data.getCure());
            deadList.add(data.getDead());
            similarList.add(data.getSimilar());
        }

        // 7.返回时局的格式容器map
        Map map =new HashMap();
        map.put("confirmList",confirmList);
        map.put("isolationList",isolationList);
        map.put("cureList",cureList);
        map.put("deadList",deadList);
        map.put("similarList",similarList);
        return map;
    }


}
